package client;

import java.io.*;
import java.net.Socket;
import java.util.Date;
import java.util.List;

import constant.Constant;
import controller.game.player.AgentImpl.ExampleAgent;
import controller.game.player.Human;
import controller.game.player.Player;
import controller.game.player.Type;
import entity.CounterMessage;
import entity.Message;
import entity.NegoStatue;
import entity.ServerMessage;
import entity.domain.DomainHelper;
import entity.evaluation.Evaluator;
import entity.evaluation.EvaluatorHelper;
import entity.offer.CounterOffer;
import entity.offer.Offer;
import server.MessageHandler;
import server.Server;
import server.SocketHandler;
import ui.MessageFrame;
import ui.message.GamePanel;
import entity.domain.Domain;

public class MyClient implements MessageHandler{

	private Socket socket;
	private String serverIPAddr;
	private int serverPort = 1234;
	private Thread socketHandler;
	private Player player;
	private String evaluationPath;
	
	public MyClient(String serverIPAddr, int serverPort, String evaluationPath){
		this.serverIPAddr = serverIPAddr;
		this.serverPort =serverPort;
		this.evaluationPath = evaluationPath;
	}

	public void connect(){
		try {
			socket = new Socket(serverIPAddr, serverPort);
			System.out.println(socket.getLocalSocketAddress());
			socketHandler = new SocketHandler(socket, this);
			socketHandler.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void handleMessage(Message message) {
		// TODO Auto-generated method stub
		player.onReceive(message);
	}

	@Override
	public Message readMessage(String socketAddr) {
		// TODO Auto-generated method stub
		CounterMessage message = null;
		message = player.getMessage();
		return message;
	}

	@Override
	public void socketHandlerEnded(Socket socket) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void handleExtraInfo(Socket socket){
		try{
			//DataInputStream dis = new DataInputStream(socket.getInputStream());
			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
			Message msg0 = (Message) ois.readObject();
			if(msg0 instanceof  ServerMessage){
				ServerMessage servMsg = (ServerMessage) msg0;
				switch (servMsg.getEvent()) {
					case Client_Connect:
						int clientNum = Integer.parseInt(servMsg.getBody());
						int roomNum = servMsg.getInfo().getRoomNum();
						//frame.receiveServMsg("您是第"+(clientNum+1)+"个玩家。");
						//初始化Palyer
						String time = servMsg.getTime();
						initPlayer(evaluationPath, roomNum, clientNum);
						//System.out.println(this.roomNum+"  %  "+this.num);
						break;
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}


	private void initPlayer(String path, int roomNum, int clientNum){
		EvaluatorHelper helper = EvaluatorHelper.createEvaluatorHelper(path, null);
		if(clientNum == Constant.NUM_OF_PLAYER_EACH_ROOM -1){
			this.player = new ExampleAgent();
		}else{
			this.player = new Human();
		}
		//this.player = new ExampleAgent();
		this.player.initPlayer(roomNum, clientNum, helper);
	}
}
